package com.chenfan.magic.tags;

import com.chenfan.magic.utils.IDKit;
import com.chenfan.magic.utils.StringKit;
import lombok.Data;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.nodes.Element;

import java.io.Serializable;

/**
 * @author lm
 * http标签模型
 * <http method="post"></http>
 */
@Data
public class TagHttp extends TagAbstract implements Serializable {

    private static final long serialVersionUID = 1L;

    public static String tagNames = "http";

    /**
     * 请求类型: get, post, ssl, 忽略大小写
     */
    protected String method;
    protected String __method;

    /**
     * 请求地址
     */
    protected String url;
    protected String __url;

    public TagHttp() {
    }

    public TagHttp(Element httpTag, int index) {
        this.element = httpTag.toString();
        this.id = httpTag.attr("id");
        if (StringKit.isBlank(this.id)) {
            this.id = IDKit.fastUUID() + "";
        }
        this.name = httpTag.attr("name");
        this.method = httpTag.attr("method");
        if (StringKit.isBlank(this.method)) {
            this.method = "get";
        }
        this.url = httpTag.attr("url");
        this.context = StringEscapeUtils.unescapeHtml4(httpTag.text());
        String sort = httpTag.attr("sort");
        if (StringKit.isBlank(sort)) {
            this.sort = index - 10000;
        } else {
            this.sort = Integer.parseInt(sort);
        }
        this.resultKey = httpTag.attr("result-key");
        if (StringKit.isBlank(this.resultKey)) {
            this.resultKey = "result_" + this.id;
        }
        String respond = httpTag.attr("respond");
        if (StringKit.isBlank(respond)) {
            this.respond = true;
        } else {
            this.respond = Boolean.valueOf(respond);
        }
        String resolve = httpTag.attr("resolve");
        if (StringKit.isBlank(resolve)) {
            this.resolve = true;
        } else {
            this.resolve = Boolean.valueOf(resolve);
        }

        this.__sort = httpTag.attr(":sort");
        this.__respond = httpTag.attr(":respond");
        this.__resolve = httpTag.attr(":resolve");
        this.__resultKey = httpTag.attr(":result-key");
        this.__method = httpTag.attr(":method");
        this.__url = httpTag.attr(":url");

        this.engine = httpTag.attr("engine");

    }
}
